const jwt = require('jsonwebtoken');
const authMiddleware = (req, res, next) => {
    // 从请求头获取 token
    const token = req.headers.authorization?.split(' ')[1];

    if (!token) {
        return res.status(401).json({
            message: '未提供访问令牌 '
        });
    }

    try {
        // 验证 token
        const decoded = jwt.verify(token, process.env.JWT_SECRET1);
        console.log(decoded);

        // 将解码后的用户信息添加到请求对象中
        req.user = decoded;
        next();
    } catch (error) {
        return res.status(401).json({
            message: '无效的访问令牌'
        });
    }
};

module.exports = authMiddleware;